package main;

import java.util.ArrayList;

public class Matrix {
	
	public ArrayList<ArrayList<Double>> matrix = new ArrayList<ArrayList<Double>>();
	public int rowsNumber;
	
	public Matrix(ArrayList<ArrayList<Double>> array) {
		matrix = array;
		rowsNumber = matrix.size();
	}
	
	public void show() {
		System.out.print('[');	
		for (int i = 0; i < rowsNumber; i++){	
			for (Double y: matrix.get(i))
				System.out.print(y + " ");
			System.out.print("| ");
		}
		System.out.println(']');	
	}
	
	public Matrix add(Matrix matrixToAdd) {
		ArrayList<ArrayList<Double>> result = new ArrayList<ArrayList<Double>>();
		
		if (matrixToAdd.rowsNumber == rowsNumber) {
			for (int i = 0; i < rowsNumber; i++){
				result.add(new ArrayList<Double>());
				for (int j = 0; j < rowsNumber; j++)
					result.get(i).add(matrix.get(i).get(j) + matrixToAdd.matrix.get(i).get(j));
			}
		}
			
		return new Matrix(result);	
	}
	
	public Matrix subtract(Matrix matrixToSubtract) {
		ArrayList<ArrayList<Double>> result = new ArrayList<ArrayList<Double>>();
		
		if (matrixToSubtract.rowsNumber == rowsNumber) {
			for (int i = 0; i < rowsNumber; i++){
				result.add(new ArrayList<Double>());
				for (int j = 0; j < rowsNumber; j++)
					result.get(i).add(matrix.get(i).get(j) - matrixToSubtract.matrix.get(i).get(j));
			}
		}
			
		return new Matrix(result);	
	}

}
